<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
	protected $userId;
	protected function isAuthorize(){
    $loginStatus=session('loginStatus');
    if(!$loginStatus){
       $this->gotoNote("未授权","/");
       exit();
     }
    $this->userId=session('userId');
    $this->userName=session('user_name');
    $this->assign('userName',$this->userName);
    $this->assign('userId',$this->userId); 
   }
    
   public function index(){
	  $this->isAuthorize();
	  $userLogObj=D("UserLog");
	  $logData=$userLogObj->getUserLog($this->userId);
	  $loginIp= ip2long($_SERVER['REMOTE_ADDR']);
	  $loginTime=time();
	  $status=$userLogObj->setUserLog($this->userId,$loginIp,$loginTime);
	  $loginIp=long2ip($logData['login_ip']);
	  $loginTime=date("Y-m-d H:i:s",$logData['login_time']);
	  $this->assign("loginIp",$loginIp);
	  $this->assign('loginTime',$loginTime);
	  $this->display('index');
	  
   }
   
   public function updateInfo(){
	   $this->isAuthorize();
	  if(empty($_POST)){
		$userObj= D('User');
	    $userData=$userObj->getUserData($this->userId);
	    $this->assign('userData',$userData);
	    $this->display('info');
		return false;
	  }
	  $userObj= D('User');
	  $status=$userObj->UpdateUserData($this->userId,$_POST);
	  if($status !==false){
	   $this->redirect('User/index','',2,'update ok');
       }else{
	   $note=$userObj->lastErrorMessage;
	    $this->gotoNote($note,'updateInfo');
       }

   }

   

   public function updatePassword(){
   $this->isAuthorize();
   if(empty($_POST)){
   	$this->display('password');
   	return false;
   }
   $userObj= new \Home\Model\UserModel();
   $status=$userObj->userModifyPassword($this->userId,$_POST);
   if($status !=false){
	   $this->redirect('User/index','',2,'update ok');
    }else{
	   $note=$userObj->lastErrorMessage;
	   $this->gotoNote($note,'updatePassword');
    }

   }
   
   	 public function gotoNote($noteString,$url){
		 $this->assign('noteString',$noteString);
		 $this->assign('url',$url);
		 $this->display('Note/note');
	 }
}